package com.example.wiki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wiki.entity.Category;
import com.example.wiki.entity.req.CategoryQueryReq;
import com.example.wiki.entity.resp.CategoryQueryResp;
import com.example.wiki.entity.resp.PageResp;
import com.example.wiki.mapper.CategoryMapper;
import com.example.wiki.service.ICategoryService;
import com.example.wiki.util.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 电子书分类 服务实现类
 * </p>
 *
 * @author jerry
 * @since 2024-10-08
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    //打印日志
    private static final Logger LOG = LoggerFactory.getLogger(ICategoryService.class);

    @Override
    public List<CategoryQueryResp> getCategoryList(CategoryQueryReq req) {
        //条件构造器
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.like(req.getName() != null && !req.getName().equals(""), "name", req.getName());
        //排序
        wrapper.orderByAsc("sort");
        List<Category> list = this.list(wrapper);
        //进行拷贝到响应封装对象中【响应数据封装】
        List<CategoryQueryResp> categoryQueryRespList = CopyUtil.copyList(list, CategoryQueryResp.class);
        //关联父分类id查询父分类的名称
        categoryQueryRespList.forEach(entity -> {
            if (entity.getParent() != 0) {//二级分类
                Category category = this.getById(entity.getParent());
                if (category != null) {
                    entity.setParentName(category.getName());
                }
            } else {//一级分类
                entity.setParentName("一级分类");
            }
        });
        //响应对象数据的设置赋值
        return categoryQueryRespList;
    }

    @Override
    public PageResp<CategoryQueryResp> getCategoryListByPage(CategoryQueryReq req) {
        //条件构造器
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.like(req.getName() != null && !req.getName().equals(""), "name", req.getName());
        //排序
        wrapper.orderByAsc("sort");
        //构建分页对象
        Page<Category> page = new Page<>(req.getPage(), req.getSize());
        //查询数据【分页查询】
        Page<Category> resultPage = this.page(page, wrapper);
        //响应对象的创建
        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        //进行拷贝到响应封装对象中【响应数据封装】
        List<CategoryQueryResp> categoryQueryRespList = CopyUtil.copyList(resultPage.getRecords(), CategoryQueryResp.class);
        //关联父分类id查询父分类的名称
        categoryQueryRespList.forEach(entity -> {
            if (entity.getParent() != 0) {//二级分类
                Category category = this.getById(entity.getParent());
                if (category != null) {
                    entity.setParentName(category.getName());
                }
            } else {//一级分类
                entity.setParentName("一级分类");
            }
        });
        LOG.info("总行数：{}", page.getTotal() + "");
        LOG.info("总页数：{}", page.getPages() + "");

        //响应对象数据的设置赋值
        pageResp.setList(categoryQueryRespList);
        pageResp.setTotal(resultPage.getTotal());
        //返回
        return pageResp;
    }

}
